﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using ShopB2C.DBUtilities;
using ShopB2C.DBUtilities.SqlDbHelper;
using ShopB2C.Entity;
using ShopB2C.Interfaces;

namespace ShopB2C.SQLDataAccess {
    public class ProductSearchKeys : IProductSearchKeys {

        #region Const Variables

        private const string _pskAppend = "Product_SearchKeys_Append";
        private const string _pskUpdateSearchNums = "Product_SearchKeys_UpdateSeachNums";
        private const string _pskDelete = "Product_SearchKeys_Delete";
        private const string _pskSelectList = "Product_SearchKeys_SelectList";
        private const string _pskSelectListByKeys = "Product_SearchKeys_SelectListByKeys";

        #endregion

        public ProductSearchKeys(string connectionString) {
            DbBase.ConnectionString = connectionString;
        }

        #region Product Search Keys

        public void ProductSearchKeysAppend(ProductSearchKeysEntity pske) {
            DbParams<SqlParameter> sqlParams = new DbParams<SqlParameter>();
            sqlParams.Add<String>("@Keys", 20, pske.Keys).SqlDbType = SqlDbType.NVarChar;
            sqlParams.Add<String>("@Description", 50, pske.Description).SqlDbType = SqlDbType.NVarChar;
            SqlDbProcedure.ExecuteNonQuery(_pskAppend, sqlParams.ToArray);
        }

        public void ProductSearchKeysUpdateSearchNums(string keys) {
            SqlDbProcedure.ExecuteNonQuery(_pskUpdateSearchNums,
                DbParams<SqlParameter>.CreateParameter<string>("@Keys", keys));
        }

        public void ProductSearchKeysDelete(string keys) {
            SqlDbProcedure.ExecuteNonQuery(_pskDelete,
                DbParams<SqlParameter>.CreateParameter<string>("@Keys", keys));
        }

        public List<ProductSearchKeysEntity> GetProductSearchKeysList(string keys) {
            List<ProductSearchKeysEntity> listResult = null;
            DbParams<SqlParameter> sqlParams = new DbParams<SqlParameter>();
            sqlParams.Add<String>("@Keys", 20, keys).SqlDbType = SqlDbType.NVarChar;
            SqlDataReader dr = SqlDbProcedure.ExecuteReader(_pskSelectListByKeys, sqlParams.ToArray);
            if (dr.HasRows) {
                listResult = new List<ProductSearchKeysEntity>();
                while (dr.Read()) {
                    listResult.Add(new ProductSearchKeysEntity() {
                        Keys = dr["Keys"].ToString(),
                        Description = dr["Description"].ToString(),
                        Totals = (int)dr["Totals"],
                        CountDate = (DateTime)dr["CountDate"],
                        SearchNums = (int)dr["SearchNums"]
                    });
                }
            }
            dr.Close();
            dr.Dispose();
            return listResult;
        }

        public List<ProductSearchKeysEntity> GetProductSearchKeysList(int pageSize, int pageIndex, out int pageCount) {
            pageCount = 0;
            List<ProductSearchKeysEntity> listResult = null;
            DbParams<SqlParameter> sqlParams = new DbParams<SqlParameter>();
            sqlParams.Add<Int32>("@PageCount", ParameterDirection.Output, 0).SqlDbType = SqlDbType.Int;
            sqlParams.Add<Int32>("@PageSize", pageSize).SqlDbType = SqlDbType.Int;
            sqlParams.Add<Int32>("@PageIndex", pageIndex).SqlDbType = SqlDbType.Int;
            SqlDataReader dr = SqlDbProcedure.ExecuteReader(_pskSelectList, sqlParams.ToArray);
            if (dr.HasRows) {
                listResult = new List<ProductSearchKeysEntity>();
                while (dr.Read()) {
                    pageCount = (int)dr["PageCount"];
                    listResult.Add(new ProductSearchKeysEntity() {
                        Keys = dr["Keys"].ToString(),
                        Description = dr["Description"].ToString(),
                        Totals = (int)dr["Totals"],
                        CountDate = (DateTime)dr["CountDate"],
                        SearchNums = (int)dr["SearchNums"]
                    });
                }
            }
            dr.Close();
            dr.Dispose();
            return listResult;
        }

        #endregion

    }
}
